package DAO;

import javabean.*;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class StaffDAO implements IStaffDAO {
    @Override
    public boolean Login(Staff staff) {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        boolean loginSuccessful = false;

        try {
            connection = ConnectManager.getConnection();
            statement = connection.prepareStatement("SELECT * FROM Staff WHERE StaffID = ? AND Password = ?");
            statement.setString(1, staff.getStaffID());
            statement.setString(2, staff.getPassword());
            resultSet = statement.executeQuery();

            if (resultSet.next()) {
                loginSuccessful = true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            ConnectManager.closeConnection(connection, statement, resultSet);
        }
        return loginSuccessful;
    }

    @Override
    public Staff getStaff(String staffID) {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        Staff staff = null;

        try {
            connection = ConnectManager.getConnection();
            statement = connection.prepareStatement("SELECT * FROM Staff WHERE StaffID = ?");
            statement.setString(1, staffID);
            resultSet = statement.executeQuery();

            if (resultSet.next()) {
                staff = new Staff();
                staff.setStaffID(resultSet.getString("StaffID"));
                staff.setStaffName(resultSet.getString("StaffName"));
                staff.setStaffPhone(resultSet.getString("StaffPhone"));
                staff.setPassword(resultSet.getString("Password"));
                staff.setAdminID(resultSet.getString("AdminID"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            ConnectManager.closeConnection(connection, statement, resultSet);
        }

        return staff;
    }

    @Override
    public boolean addPendingStaff(Staff staff) {
        Connection connection = null;
        PreparedStatement statement = null;
        boolean success = false;

        try {
            connection = ConnectManager.getConnection();
            //查找再Staff表中是否有相同的ID
            statement = connection.prepareStatement("SELECT * FROM Staff WHERE StaffID = ?");
            statement.setString(1, staff.getStaffID());
            ResultSet resultSet = statement.executeQuery();
            if (resultSet.next()) {
                return false;
            }
            statement = connection.prepareStatement("INSERT INTO PendingStaff (StaffID, StaffName, StaffPhone, Password) VALUES ( ?, ?, ?, ?)");
            statement.setString(1, staff.getStaffID());
            statement.setString(2, staff.getStaffName());
            statement.setString(3, staff.getStaffPhone());
            statement.setString(4, staff.getPassword());

            int rowsInserted = statement.executeUpdate();
            if (rowsInserted > 0) {
                success = true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            ConnectManager.closeConnection(connection, statement, null);
        }

        return success;
    }

    @Override
    public boolean deleteStaff(String staffID) {
        Connection connection = null;
        PreparedStatement statement = null;
        boolean success = false;

        try {
            connection = ConnectManager.getConnection();
            statement = connection.prepareStatement("DELETE FROM Staff WHERE StaffID = ?");
            statement.setString(1, staffID);

            int rowsDeleted = statement.executeUpdate();
            if (rowsDeleted > 0) {
                success = true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            ConnectManager.closeConnection(connection, statement, null);
        }

        return success;
    }

    @Override
    public boolean updateStaff(Staff staff) {
        Connection connection = null;
        PreparedStatement statement = null;
        boolean success = false;

        try {
            connection = ConnectManager.getConnection();
            statement = connection.prepareStatement("UPDATE Staff SET StaffName = ?, StaffPhone = ?, Password = ?, AdminID = ? WHERE StaffID = ?");
            statement.setString(1, staff.getStaffName());
            statement.setString(2, staff.getStaffPhone());
            statement.setString(3, staff.getPassword());
            statement.setString(4, staff.getAdminID());
            statement.setString(5, staff.getStaffID());

            int rowsUpdated = statement.executeUpdate();
            if (rowsUpdated > 0) {
                success = true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            ConnectManager.closeConnection(connection, statement, null);
        }

        return success;
    }

    @Override
    public List<Staff> getAllStaff() {
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;
        List<Staff> staffList = new ArrayList<>();

        try {
            connection = ConnectManager.getConnection();
            statement = connection.createStatement();
            resultSet = statement.executeQuery("SELECT * FROM Staff");

            while (resultSet.next()) {
                Staff staff = new Staff();
                staff.setStaffID(resultSet.getString("StaffID"));
                staff.setStaffName(resultSet.getString("StaffName"));
                staff.setStaffPhone(resultSet.getString("StaffPhone"));
                staff.setPassword(resultSet.getString("Password"));
                staff.setAdminID(resultSet.getString("AdminID"));

                staffList.add(staff);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            ConnectManager.closeConnection(connection, statement, resultSet);
        }

        return staffList;
    }

    @Override
    public List<Staff> getAllPendingStaff() {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        List<Staff> pendingStaffList = new ArrayList<>();

        try {
            connection = ConnectManager.getConnection();
            statement = connection.prepareStatement("SELECT * FROM PendingStaff");
            resultSet = statement.executeQuery();

            while (resultSet.next()) {
                Staff staff = new Staff();
                staff.setPendingID(resultSet.getInt("PendingID"));
                staff.setStaffID(resultSet.getString("StaffID"));
                staff.setStaffName(resultSet.getString("StaffName"));
                staff.setStaffPhone(resultSet.getString("StaffPhone"));
                staff.setPassword(resultSet.getString("Password"));
                pendingStaffList.add(staff);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            ConnectManager.closeConnection(connection, statement, resultSet);
        }

        return pendingStaffList;
    }
}
